package com.da.web.dorm_leave.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.da.common.Result;
import com.da.web.dorm_leave.entity.DormLeave;
import com.da.web.dorm_leave.entity.LeaveParm;
import com.da.web.dorm_leave.service.DormLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author da
 * @time 2023/12/14 下午 3:59
 */
@RestController
@RequestMapping("/api/leave")
public class DormLeaveController {
    @Autowired
    private DormLeaveService dormLeaveService;

    //新增
    @PostMapping
    public Result add(@RequestBody DormLeave dormLeave) {
        boolean save = dormLeaveService.save(dormLeave);
        if (save) {
            return Result.success("新增成功");
        }
        return Result.error("新增失败");
    }

    //编辑
    @PutMapping
    public Result edit(@RequestBody DormLeave dormLeave) {
        boolean b = dormLeaveService.updateById(dormLeave);
        if (b) {
            return Result.success("编辑成功");
        }
        return Result.error("编辑失败");
    }

    @DeleteMapping("/{leaveId}")
    public Result delete(@PathVariable("leaveId") Long leaveId) {
        boolean b = dormLeaveService.removeById(leaveId);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //列表
    @GetMapping("/list")
    public Result list(LeaveParm parm) {
        IPage<DormLeave> list = dormLeaveService.pageList(parm);
        return Result.success("查询成功", list);
    }

}
